/* Copyright (C) 2007-2010 STS Soft

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


namespace STSdb.Persist
{
    //public class XKeyIndexerPersist : IIndexerPersist<XKey>
    //{
    //    #region IIndexerPersist<XKey> Members

    //    public void Store(BinaryWriter writer, IIndexer<XKey> values)
    //    {
    //        int subCount = values[0].SubKeys.Length;
    //        CountCompression.Serialize(writer, (long)subCount);

    //        for (int i = 0; i < subCount; i++)
    //        {
    //            XSubKey subKey = values[0].SubKeys[i];
    //            Type type = subKey.Value.GetType();
    //            TypeCode code = Type.GetTypeCode(type);
    //            if (code == TypeCode.Object)
    //                throw new NotSupportedException("XKeyIndexerPersist not support subkeys of type " + type.ToString());

    //            writer.Write(subKey.Flags);
    //            writer.Write((byte)code);
                
    //            Type persistType = typeof(PrimitiveIndexerPersist<>).MakeGenericType(type);
    //            object persistInstance = Activator.CreateInstance(persistType);

    //            Type internalType = typeof(InternalIndexer<>).MakeGenericType(type);
    //            object internalInstance = Activator.CreateInstance(internalType, values, i, subKey.Flags);

    //            MethodInfo storeMethod = persistType.GetMethod("Store");
    //            storeMethod.Invoke(persistInstance, new object[] { writer, internalInstance });
    //        }
    //    }

    //    public void Load(BinaryReader reader, IIndexer<XKey> values)
    //    {
    //        int subCount = (int)CountCompression.Deserialize(reader);

    //        for (int i = 0; i < values.Count; i++)
    //            values[i].SubKeys = new XSubKey[subCount];

    //        for (int i = 0; i < subCount; i++)
    //        {
    //            byte flags = reader.ReadByte();
    //            TypeCode code = (TypeCode)reader.ReadByte();
    //            Type type = code.ToType(); //extension

    //            Type persistType = typeof(PrimitiveIndexerPersist<>).MakeGenericType(type);
    //            object persistInstance = Activator.CreateInstance(persistType);

    //            Type internalType = typeof(InternalIndexer<>).MakeGenericType(type);
    //            object internalInstance = Activator.CreateInstance(internalType, values, i, flags);

    //            MethodInfo loadMethod = persistType.GetMethod("Load");
    //            loadMethod.Invoke(persistInstance, new object[] { reader, internalInstance });
    //        }
    //    }

    //    #endregion

    //    private class InternalIndexer<T> : IIndexer<T>
    //    {
    //        public readonly IIndexer<XKey> Indexer;
    //        public readonly int Idx;
    //        public readonly byte Flags;

    //        public InternalIndexer(IIndexer<XKey> indexer, int idx, byte flags)
    //        {
    //            Indexer = indexer;
    //            Idx = idx;
    //            Flags = flags;
    //        }

    //        #region IIndexer<T> Members

    //        public T this[int index]
    //        {
    //            get { return (T)Indexer[index].SubKeys[Idx].Value; }
    //            set { Indexer[index].SubKeys[Idx] = new XSubKey(value, Flags); }
    //        }

    //        public int Count
    //        {
    //            get { return Indexer.Count; }
    //        }

    //        #endregion
    //    }
    //}
}
